package com.dcivision.dms.core;

import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.document.Document;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.framework.FullTextIndexFactory;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.IndexManager;

/**
 * <p>Class Name:       IndexBackgroundThread.java    </p>
 * <p>Description:      The class mainly run in thread to handle lucene document create, update and delete
 *                      operation
 *  </p>
 * @author              Zoe Shum
 * @company             DCIVision Limited
 * @creation date       01/08/2003
 * @version             $Revision: 1.7 $
 * @deprecated This class has deprecated.
 */

public class IndexBackgroundThread extends Thread {

  public static final String REVISION = "$Revision: 1.7 $";


  protected Log log = LogFactory.getLog(this.getClass().getName());
  private String mode = null;
  private String[] fields = null;
  private InputStream data = null;
  private DmsDocument dmsDocument = null;

  /**
   * Class constructor specifying the dmsDocument object, inputStream data, mode and lucene document field
   * to be insert, update or delete.
   *  
   * @param dmsDocument the dmsDocument object
   * @param data        the inputStream of Full text data
   * @param mode        Operation mode, GlobalConstant.OP_MODE_UPDATE, GlobalConstant.OP_MODE_UPDATE or 
   *                    GlobalConstant.OP_MODE_DELETE         
   * @param fields      lucene document fields
   * @see com.dcivision.framework.GlobalConstant
   * @see com.dcivision.dms.core.DmsIndexConverter
   */
  public IndexBackgroundThread(DmsDocument dmsDocument, InputStream data, String mode, String[] fields) {
    super();
    this.dmsDocument = dmsDocument;
    this.data = data;
    this.mode = mode;
    this.fields = fields;
  }

  public void run() {
    try {
      log.info("IndexBackgroundThread Begin.");
      IndexManager idxHandler = FullTextIndexFactory.getInstance();
      if (GlobalConstant.OP_MODE_INSERT.equals(mode)) {
        log.info("indexDocument on insert Begin... data = " + (data==null));
        idxHandler.insertDocument(DmsIndexConverter.getIndexDocument(dmsDocument, data));
      } else if (GlobalConstant.OP_MODE_UPDATE.equals(mode)) {
        log.info("indexDocument on update Begin.");
        Document doc = DmsIndexConverter.getIndexDocument(dmsDocument, data, fields);
        idxHandler.updateDocument(doc);
      } else if (GlobalConstant.OP_MODE_DELETE.equals(mode)) {
        log.info("indexDocument on delete Begin.");
        idxHandler.deleteDocument(DmsIndexConverter.getIndexDocument(dmsDocument, data));
      }
      log.info("IndexBackgroundThread End.");
    } catch (Exception e) {
      log.error("Error when indexing document.", e);
    }finally{
      try { data.close(); } catch (Exception ignore) {} finally { data = null; }
    }
  }

  public void finalize() {
    this.destroy();
  }

  public void destroy() {
    try { this.data.close(); } catch (Exception ignore) {} finally { this.data = null; }
    this.dmsDocument = null;
    this.mode = null;
    this.fields = null;
  }

}
